<?php
namespace Home\Controller;
use Think\Controller;
use Home\Model\RecycleOrderModel;
use Home\Model\RegionModel;
use Home\Model\ProvinceModel;
use Home\Model\CityModel;
use Home\Model\MemberModel;
use Admin\Controller\AjaxController;
use Admin\Model\VersionModel;
use Admin\Model\OptionCategoryModel;
use Admin\Model\OptionModel;
/**
 * 手机class
 * 包含手机回收和手机维修
 */

class UserController extends UserBaseController{

	private $RecycleOrder = null;
	function __construct(){
		parent::__construct();
		$this->RecycleOrder = new RecycleOrderModel();
	}

	/**
	 * 用户获取我的回收订单列表
	 */
	public function get_order_list(){
		// 回收订单
		$recycle_order = $this->RecycleOrder->get_user_order($this->uid);
		returnMobileJson(200,'获取成功',$recycle_order);
	}

	/**
	 * 用户获取订单详情
	 */
	public function get_order(){
		$rid=$_POST['rid'];
		if(empty($rid)){
			returnMobileJson(201,'参数错误');
		}
		$recycle_order = $this->RecycleOrder->get_order($rid);
		if($recycle_order['mid']){
			$member_db = new MemberModel();
			$master_info = $member_db->access_to_master_information($recycle_order['mid']);
			$recycle_order['master_info'] = $master_info;
		}else{
			$recycle_order['master_info'] = '';
		}


		returnMobileJson(200,'获取成功',$recycle_order);
	}

	/**
     * 获取品牌机型
     */
	public function get_brand_version(){
		$Ajax = new AjaxController();
		$Ajax->get_brand_version();
	}

	/**
     * 获取手机选项
     */
	public function get_mp_option(){
		$vid = $_POST['vid'];
		if(empty($vid)){
			returnMobileJson(201,'参数错误');
		}
		$OptionCategory =new OptionCategoryModel();
		$Option = new OptionModel();

		$option_categorys = $OptionCategory->get_option_category($vid);
		foreach ($option_categorys as $key => $option_category) {
			$option_categorys[$key]['option'] = $Option->get_option($option_category['cid']);
		}

		returnMobileJson(200,'获取成功',$option_categorys);
	}

	/**
     * 提交手机信息
     */
	public function set_mp_info(){
		$vid = $_POST['vid'];
		$mp_info = json_decode($_POST['mp_info'],true);
		if(empty($vid) || empty($mp_info)){
			returnMobileJson(201,'参数错误');
		}
		$Option = new OptionModel();
		foreach ($mp_info as $key => $value) {
			$option = $Option->get_option_price($value);
			if(empty($option)){
				returnMobileJson(201,'参数错误');
			}
		}
		$data['uid'] = $this->uid;
		$data['vid'] = $vid;
		$data['mp_info'] = json_encode($mp_info);
		$rid = $this->RecycleOrder->set_order($data);
		$result['rid'] = $rid;
//		echo $rid;
		if($rid){
			returnMobileJson(200,'提交成功',$result);
		}else{
			returnMobileJson(202,'提交失败');
		}
	}

	/**
     * 获取回收费用预估
     */
	public function get_valuation(){
		$rid=$_POST['rid'];
		if(empty($rid)){
			returnMobileJson(201,'参数错误');
		}

		$recycle_order = $this->RecycleOrder->get_order($rid);


		$Version = new VersionModel();
		$version_info = $Version->get_version_info($recycle_order['vid']);
		$mp_info = json_decode($recycle_order['mp_info'],true);
		$Option = new OptionModel();
		$option_price = 0;
		foreach ($mp_info as $key => $oid) {
			$option_price += $Option->get_option_price($oid)['o_price'];
		}

		$valuation = $version_info['v_max_price']-$option_price;

		$valuation = $valuation >= $version_info['v_min_price']?$valuation:$version_info['v_min_price'];

		$data['valuation'] = $valuation;
		$data['v_name'] = $recycle_order['v_name'];
		returnMobileJson(200,'获取成功',$data);
	}

	/**
     * 提交回收订单
     */
	public function add_recycle_order(){
		$rid = $_POST['rid'];
		$data['repair_way'] = $_POST['repair_way'];
		$data['phone_number'] = $_POST['mobphone'];
		$vcode = $_POST['vcode'];
		if($data['repair_way'] == 1){
			$data['door_time'] = $_POST['door_time'];
		}else{
			$data['member_id'] = $_POST['member_id'];
		}
		$data['region'] = $_POST['region'];
		$data['address'] = $_POST['address'];
		$data['note'] = $_POST['note'];
		$data['status'] = 0;
		$data['update_time'] = time();
		$data['order_time'] = date('Y-m-d H:i:s');
		$data['city_id'] = $_POST['city_id'];
		$data['price'] = $_POST['valuation'];
				// 订单编号
		// 取用户ID后4位
//		$uid_4 = substr($this->uid,-4);
		$data['order_num'] = date('YmdHis',time()).rand(1000,9999).$rid;

		$data['valuation'] = $_POST['valuation'];
		if($data['repair_way']==1){
			if(empty($rid)||empty($data['repair_way'])||empty($data['phone_number'])
					||(empty($data['door_time'])&&empty($data['member_id']))
					||empty($data['region'])||empty($data['address'])||empty($data['valuation'])){
				returnMobileJson(201,'参数错误');
			}
		}else{
			if(empty($rid)||empty($data['repair_way'])||empty($data['phone_number']) ||empty($data['member_id']) ||empty($data['region'])||empty($data['valuation'])){
				returnMobileJson(206,'参数错误');
			}
		}




		if(!preg_match("/^1\d{10}$/",$data['phone_number'])){
			returnMobileJson(202,'手机号码格式错误');
		}



		if($vcode && $vcode != get_msgcode($data['phone_number'])){
			returnMobileJson(203,'短信验证码错误');
		}




		//自动指派订单
		if($data['repair_way'] == 1){
//			$type = array('3','5','4');
//			$data['mid'] = assgin_engineer($type);
		}else{
			$data['status'] = 1;
			$data['mid'] = $_POST['member_id'];
			if(empty($data['mid'])){
				returnMobileJson(202,'参数错误');
			}
		}


		if($this->RecycleOrder->set_order($data,$rid)){
			$data = $this->RecycleOrder->get_order($rid);
			R('Wx/sendUserOrder',array($this->uid,$data));
			returnMobileJson(200,'提交成功');
		}else{
			returnMobileJson(204,'提交失败');
		}
 	}

	/**
	 * 客户同意价格回收
	 */
	public function agreed_price(){
		$rid=$_POST['rid'];
		if(empty($rid)){
			returnMobileJson(201,'参数错误');
		}
		$data['status']=6;
		if($this->RecycleOrder->set_order($data,$rid)){
			returnMobileJson(200,'修改成功');
		}else{
			returnMobileJson(202,'修改失败');
		}
	}

	/**
	 * 客户取消订单
	 */
	public function cancel_order(){
		$rid=$_POST['rid'];
		if(empty($rid)){
			returnMobileJson(201,'参数错误');
		}
		$data['status']=4;
		if($this->RecycleOrder->set_order($data,$rid)){
			returnMobileJson(200,'修改成功');
		}else{
			returnMobileJson(202,'修改失败');
		}
	}

	/**
	 * 客户评价
	 */
	public function evaluate(){
		$rid=$_POST['rid'];
		$data['gojiw_practicability'] = $_POST['practicability'];
		$data['gojiw_usability'] = $_POST['usability'];
		$data['gojiw_safety'] = $_POST['safety'];
		$data['master_quality'] = $_POST['quality'];
		$data['master_attitude'] = $_POST['attitude'];
		$data['master_efficiency'] = $_POST['efficiency'];
		$data['evaluate'] = $_POST['evaluate'];

		$order_data = $this->RecycleOrder->get_order($rid);

		$data_synchronization['practicability'] =  $_POST['practicability'];
		$data_synchronization['usability'] =  $_POST['usability'];
		$data_synchronization['safety'] =  $_POST['safety'];
		$data_synchronization['quality'] =  $_POST['quality'];
		$data_synchronization['attitude'] =  $_POST['attitude'];
		$data_synchronization['efficiency'] =  $_POST['efficiency'];
		$data_synchronization['uid'] = $this->uid;
		$data_synchronization['content'] = $_POST['evaluate'];
		$data_synchronization['order_id'] = $_POST['rid'];
		$data_synchronization['type'] = 2;
		$data_synchronization['phone'] = $order_data['phone_number'];
		$data_synchronization['ctime'] = time();



		if(empty($rid)||empty($_POST['practicability'])||empty($_POST['usability'])
			||empty($_POST['safety'])||empty($_POST['quality'])||empty($_POST['attitude'])
			||empty($_POST['efficiency'])||empty($data['evaluate'])){
			returnMobileJson(201,'参数错误');
		}

		M('comment')->add($data_synchronization);

		$data['status']=7;
		if($this->RecycleOrder->set_order($data,$rid)){
			returnMobileJson(200,'修改成功');
		}else{
			returnMobileJson(202,'修改失败');
		}
	}

	/**
	 * 获取省份
	 */
	public function get_province(){
		$Province = new ProvinceModel();
		if($province = $Province->get_province())
			returnMobileJson(200,'获取成功',$province);
		else
			returnMobileJson(201,'没有数据');
	}

	/**
	 * 获取营业状态的城市
	 * @param  int $pid 省份id
	 */
	public function get_city($pid){
		$City = new CityModel();
		if($city = $City->get_city($pid))
			returnMobileJson(200,'获取成功',$city);
		else
			returnMobileJson(201,'没有数据');
	}

	/**
	 * 获取开放区域
	 */
	public function get_region($city_id){
		$Region = new RegionModel();
		if($region = $Region->get_region($city_id))
			returnMobileJson(200,'获取成功',$region);
		else
			returnMobileJson(201,'没有数据');
	}

	/**
	 * 获取联盟商户门店列表
	 */
	public function get_union_store($city_id){
		$Member = new MemberModel();
		if($union_store_list = $Member->get_member_list(6,$city_id))
			returnMobileJson(200,'获取成功',$union_store_list);
		else
			returnMobileJson(201,'没有数据');
	}

	/**
	 * 发送短信验证码
	 * @param  int $mobphone 手机号
	 * @return [type]           [description]
	 */
	public function send_msgcode(){
		$mobphone = $_REQUEST['phone'];
		if(!preg_match("/^1\d{10}$/",$mobphone)){
			returnMobileJson(201,'手机号码格式错误');
		}
		echo send_msgcode($mobphone);
	}

	public function exit_login(){
		$_SESSION['master_id'] = '';
		$_SESSION['admin_id'] = '';
		$_SESSION['uid'] = '';
	}


	public function master_login(){
		$_SESSION['master_id'] = 113;
	}
}